package cn.edu.scau.cmi.liuxintong;
import java.awt.event.ActionEvent;

import cn.edu.scau.cmi.liuxintong.Singleton.Marshal;
import cn.edu.scau.cmi.liuxintong.Singleton.Chairman;

import cn.edu.scau.cmi.liuxintong.Singleton.Chairman;
import cn.edu.scau.cmi.liuxintong.Singleton.Marshal;
import cn.edu.scau.cmi.liuxintong.Singleton.Chairman;

import javafx.application.*;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.stage.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.HTMLEditor;
public class SingletonClient extends Application {
	private int count1=0;
	private int count2=0;
	
	public void start(Stage primaryStage) throws Exception {
		 AnchorPane anchorPane = new AnchorPane();
         Chairman instance = null;
         Marshal testLis = null;
		 Label label1 = new Label("请输入主席名称:");
		 Label label2 = new Label("请输入元帅名称:");
		 TextField textField1 = new TextField();
		 TextField textField2 = new TextField();
		 Button button1 = new Button("创建");
		 Button button2 = new Button("创建");
		 TextArea textArea = new TextArea();
	        textArea.setPrefHeight(380);
	        textArea.setPrefWidth(680);
		 anchorPane.getChildren().addAll(label1,label2,textField1,textField2,button1,button2,textArea);
		 button1.setOnAction(new EventHandler() {
				public void handle(Event event) {
					 if(textField1.getText()!=null&&!textField1.getText().isEmpty()) {
						if(count1==0) {
							instance.getInstance(textField1.getText());
						    count1++;
						 textArea.appendText(textField1.getText()+"cn.edu.scau.cmi.liuxintong.Singleton.Chairman\n");
					 }
					 else {
						    textArea.appendText("单例模式下已有对象创建，无法创建新对象了！你现在获得的对象是：\n");
						    textArea.appendText(instance.getName()+"cn.edu.scau.cmi.liuxintong.Singleton.Chairman\n");
					 }
				}
				}
			});
		 button2.setOnAction(new EventHandler() {
			public void handle(Event event) {
				 if(textField2.getText()!=null&&!textField2.getText().isEmpty()) {
					if(count2<10) {
						if(testLis.addMarshal(textField2.getText())==-1) {//无该元帅且没到10个
							count2++;
							 textArea.appendText(textField2.getText()+"cn.edu.scau.cmi.liuxintong.Singleton.Chairman\n");
						}
						else {
							 textArea.appendText("多例模式下已创建该对象，无法创建重复对象了！\n");
				 }
					}
				 else {
					    textArea.appendText("多例模式下10个对象创建，无法创建新对象了！\n");
				 }
				 }
			}
		});
		
		//界面布局
		 AnchorPane.setLeftAnchor(label1, 10.0);
		 AnchorPane.setBottomAnchor(label1,480.0);//下
		 AnchorPane.setLeftAnchor(textField1, 140.0);
		 AnchorPane.setBottomAnchor(textField1,470.0);//下
		 AnchorPane.setLeftAnchor(button1,350.0);
		 AnchorPane.setBottomAnchor(button1,470.0);//下
		 AnchorPane.setLeftAnchor(label2, 10.0);
		 AnchorPane.setBottomAnchor(label2,400.0);//下
		 AnchorPane.setLeftAnchor(textField2, 140.0);
		 AnchorPane.setBottomAnchor(textField2,400.0);//下
		 AnchorPane.setLeftAnchor(button2,350.0);
		 AnchorPane.setBottomAnchor(button2,400.0);//下
		 AnchorPane.setLeftAnchor(textArea, 10.0);
		 AnchorPane.setBottomAnchor(textArea,10.0);
		 primaryStage.setScene(new Scene(anchorPane, 700, 500));
	        primaryStage.show();
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 launch(args);
	}
}
